package flc.ast.activity;

import androidx.annotation.LayoutRes;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.stark.jigsaw.puzzle.PuzzleLayout;
import com.stark.jigsaw.puzzle.PuzzleUtils;
import com.stark.jigsaw.puzzle.adapter.PuzzleLayoutAdapter;
import com.stark.jigsaw.puzzle.template.slant.NumberSlantLayout;
import com.stark.jigsaw.puzzle.template.straight.NumberStraightLayout;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

import flc.ast.BaseAc;
import flc.ast.HomeActivity;
import flc.ast.R;
import flc.ast.databinding.ActivityPuzzleBinding;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.DensityUtil;
import stark.common.basic.utils.RxUtil;

public class PuzzleActivity extends BaseAc<ActivityPuzzleBinding> {

    public static List<String> listPath = new ArrayList<>();
    private int defaultSel = 0;
    private List<Bitmap> mBmpList = new ArrayList<>();
    private PuzzleLayout puzzleLayout;


    @Override
    protected int onCreate() {
        return R.layout.activity_puzzle;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);
        mDataBinding.ivPuzzleBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.ivPuzzleSave.setOnClickListener(this);
        //获取并设置图片拼图
        setPic();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivPuzzleSave://保存
                saveImg();
                break;
        }
    }

    @Override
    protected void initData() {
        //获取拼图样式
        mDataBinding.rvPuzzleList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        PuzzleLayoutAdapter adapter = new PuzzleLayoutAdapter(getPuzzleItemLayoutId());
        adapter.setListDatas(PuzzleUtils.getPuzzleLayouts(listPath.size()));
        adapter.setListener(puzzleLayout -> {
            int themeType = 0;
            int themeId = 0;
            if (puzzleLayout instanceof NumberSlantLayout) {
                themeType = 0;
                themeId = ((NumberSlantLayout) puzzleLayout).getTheme();
            } else if (puzzleLayout instanceof NumberStraightLayout) {
                themeType = 1;
                themeId = ((NumberStraightLayout) puzzleLayout).getTheme();
            }
            mDataBinding.myPuzzleView.setPuzzleLayout(PuzzleUtils.getPuzzleLayout(themeType, listPath.size(), themeId));
            mDataBinding.myPuzzleView.addPieces(mBmpList);
        });
        mDataBinding.rvPuzzleList.setAdapter(adapter);
        int type = 1;
        puzzleLayout = PuzzleUtils.getPuzzleLayout(type, listPath.size(), defaultSel);//默认选中的效果
        mDataBinding.myPuzzleView.setPuzzleLayout(puzzleLayout);
    }

    //保存图片
    private void saveImg() {
        showDialog(getString(R.string.saving));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<String>() {

                    @Override
                    public void doBackground(ObservableEmitter<String> emitter) {
                        ImageUtils.save2Album(ImageUtils.view2Bitmap(mDataBinding.myPuzzleView), Bitmap.CompressFormat.PNG);
                        emitter.onNext("");
                    }

                    @Override
                    public void accept(String path) {
                        dismissDialog();
                        ToastUtils.showShort(getString(R.string.save_success));
                        startActivity(HomeActivity.class);
                    }
                });
            }
        }, 1000);
    }

    //拼图样式颜色
    protected @LayoutRes
    int getPuzzleItemLayoutId() {
        return R.layout.item_jigsaw_puzzle_layout_light;
    }


    //获取并设置拼图
    private void setPic() {
        RxUtil.create(new RxUtil.Callback<Boolean>() {
            @Override
            public void doBackground(ObservableEmitter<Boolean> emitter) {
                Bitmap bitmap = null;
                int width = DensityUtil.getWith(mContext) / 2;
                int height = DensityUtil.getHeight(mContext) / 2;
                for (String path : listPath) {
                    bitmap = getScaleBitmap(path, width, height);
                    mBmpList.add(bitmap);
                }
                emitter.onNext(true);
            }

            @Override
            public void accept(Boolean obj) {
                dismissDialog();
                mDataBinding.myPuzzleView.post(new Runnable() {
                    @Override
                    public void run() {
                        mDataBinding.myPuzzleView.addPieces(mBmpList);
                    }
                });
            }
        });
    }

    private Bitmap getScaleBitmap(String path, int width, int height) {
        try {
            return Glide.with(this).asBitmap().load(path).submit(width, height).get();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return Bitmap.createScaledBitmap(BitmapFactory.decodeFile(path), width, height, true);
    }

}